package com.bkgy.modularbx.temperature1870.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.commom.model.QueryDTO;
import com.bkgy.modularbx.temperature1870.domain.FurnaceTemperature1870;
import com.bkgy.modularbx.temperature1870.dto.SearchCondition1870;
import com.bkgy.modularbx.temperature1870.mapper.FurnaceTemperatureMapper1870;
import com.bkgy.modularbx.temperature1870.service.FurnaceTemperatureService1870;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class DataCollectionService1870Impl extends ServiceImpl<FurnaceTemperatureMapper1870, FurnaceTemperature1870> implements FurnaceTemperatureService1870
{
    // 定义数据库时间格式
    private static final DateTimeFormatter DB_DATE_TIME_FORMAT =
            DateTimeFormatter.ofPattern("yyyy-MM-dd-HH.mm.ss.SSSSSS");

    @Override
    public ResultData<Page<FurnaceTemperature1870>> pageDataCollection(QueryDTO<SearchCondition1870> queryDTO) {
        try {
            SearchCondition1870 condition1870 = queryDTO.getDto();
            LambdaQueryWrapper<FurnaceTemperature1870> wrapper = new LambdaQueryWrapper<>();

            // 构建查询条件
            if (condition1870 != null) {
                wrapper.like(StringUtils.hasText(condition1870.getExtId()), FurnaceTemperature1870::getExtId, condition1870.getExtId());
                wrapper.like(StringUtils.hasText(condition1870.getEntId()), FurnaceTemperature1870::getEntId, condition1870.getEntId());
                wrapper.like(StringUtils.hasText(condition1870.getSteelGrade()), FurnaceTemperature1870::getSteelGrade, condition1870.getSteelGrade());
                wrapper.ge(StringUtils.hasText(condition1870.getStartTime()), FurnaceTemperature1870::getCreateTime, DateTimeFormatter.ofPattern(condition1870.getStartTime()));
                wrapper.le(StringUtils.hasText(condition1870.getEndTime()), FurnaceTemperature1870::getCreateTime, DateTimeFormatter.ofPattern(condition1870.getEndTime()));
            }
            Page<FurnaceTemperature1870> page = new Page<>(queryDTO.getPage().getPageIndex(), queryDTO.getPage().getPageSize());
            Page<FurnaceTemperature1870> result = page(page, wrapper);
            List<FurnaceTemperature1870> listForPage = new ArrayList<>();
            for (FurnaceTemperature1870 obj : page.getRecords()) {
                double comJudge = 0.0;
                comJudge += getDoubleValue(obj.getP4HeartRate());
                comJudge += getDoubleValue(obj.getP5SoakRate());
                comJudge += getDoubleValue(obj.getP6ScsRate());
                obj.setComJudge(comJudge);
                listForPage.add(obj);
            }
            return ResultData.ResultDataSuccess(listForPage, (int) result.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("获取失败" + e.getMessage(), null);
        }
    }

    /**
     * double值转换
     *
     * @param value 数值
     * @return 数值（double）
     */
    private double getDoubleValue(Object value) {
        if (value == null) {
            return 0.0;
        }
        if (value instanceof Number) {
            return ((Number) value).doubleValue();
        }
        try {
            return Double.parseDouble(value.toString());
        } catch (NumberFormatException e) {
            return 0.0;
        }
    }
}
